package fpt.edu.vn.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.AddnewDao;
import fpt.edu.vn.dao.UsersDao;
import fpt.edu.vn.model.Users;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class UserLoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;
	private String displayName;
	private String avatar;
	private String password;
	private int userIdss;
	private String curUrl;
	private List<Users> users;
	private String errorString="Email hoặc password không đúng!";
	private Users user = new Users();
	private AddnewDao addnewdao = new AddnewDao();

	private UsersDao dao = new UsersDao();
	// For SessionAware
	Map<String, Object> session;

	public String execute() {
		
			this.users = addnewdao.getAdminlogin(email, password);
			if (users.size() > 0) {
				if(users.get(0).getIsBanner()==1){
					errorString="Tài khoản của bạn đã bị khoá!";
					return ERROR;
				}
				if (users.get(0).getIsBanner()!=1) {
					setUserIdss(this.users.get(0).getUsersId());
					setDisplayName(this.users.get(0).getDisplayName());
					session = ActionContext.getContext().getSession();
					if(session.containsKey("curUrl")){
						curUrl = session.get("curUrl").toString();
					}
					
					if(curUrl==null){
						curUrl = "index.action";
					}
					session.put("displayNamess", (String) displayName);
					session.put("emailss", (String) email);
					session.put("userIdss", userIdss);
					session.put("userRoleIdss", users.get(0).getRoleId());
					dao.updateLastLogin(userIdss);
				}
			} else {
				return ERROR;
			}
			return SUCCESS;
		
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}
	
	public String getErrorString() {
		return errorString;
	}

	public void setErrorString(String errorString) {
		this.errorString = errorString;
	}
	
	public String getCurUrl() {
		return curUrl;
	}

	public void setCurUrl(String curUrl) {
		this.curUrl = curUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public int getUserIdss() {
		return userIdss;
	}

	public void setUserIdss(int userIdss) {
		this.userIdss = userIdss;
	}

	public List<Users> getUsers() {
		return users;
	}

	public void setUsers(List<Users> users) {
		this.users = users;
	}

}
